/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.ruang;

import com.ivanbiz.injection.CallCtx;
import com.ivanbiz.properties.SISBeanConstants;
import com.ivanbiz.properties.SISConstants;
import com.ivanbiz.properties.SISScreenConstants;
import com.ivanbiz.tools.IvanBiz;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Ivan
 */
public class RuangAction extends IvanBiz {

    ActionMessages actionMessages = new ActionMessages();

    public ActionForward ruangList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        RuangService ruangService = (RuangService) CallCtx.getContext(request).getBean(SISBeanConstants.RUANG_SERVICE);
        try {
            List listRuang = ruangService.loadAll();
            request.setAttribute(SISConstants.LIST_RUANG, listRuang);
        } catch (Exception ex) {
            Logger.getLogger(RuangAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.RUANG_LIST);
    }

    public ActionForward ruangAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        RuangService ruangService = (RuangService) CallCtx.getContext(request).getBean(SISBeanConstants.RUANG_SERVICE);
        RuangForm ruangForm = (RuangForm) form;
        try {
            boolean kodeRuangIsUsed = ruangService.cekIsEmptyKode(ruangForm.getKodeRuang());
            if (kodeRuangIsUsed) {
                actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.CODE_IS_USED));
                saveMessages(request, actionMessages);
                return mapping.findForward(SISScreenConstants.RUANG_LIST);
            } else {
                Ruang ruang = new Ruang();
                ruang.setKodeRuang(ruangForm.getKodeRuang());
                ruang.setNamaRuang(ruangForm.getNamaRuang());
                ruang.setDeskripsi(ruangForm.getDeskripsi());
                ruangService.saveRuang(ruang);
                ruangForm.reset(mapping, request);
            }
        } catch (Exception ex) {
            Logger.getLogger(RuangAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.RUANG);
    }

    public ActionForward ruangEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        RuangService ruangService = (RuangService) CallCtx.getContext(request).getBean(SISBeanConstants.RUANG_SERVICE);
        RuangForm ruangForm = (RuangForm) form;
        try {
            Ruang ruang = ruangService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            ruangForm.setKodeRuang(ruang.getKodeRuang());
            ruangForm.setNamaRuang(ruang.getNamaRuang());
            ruangForm.setDeskripsi(ruang.getDeskripsi());
        } catch (Exception ex) {
            Logger.getLogger(RuangAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.RUANG_EDIT);
    }

    public ActionForward ruangUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        RuangService ruangService = (RuangService) CallCtx.getContext(request).getBean(SISBeanConstants.RUANG_SERVICE);
        RuangForm ruangForm = (RuangForm) form;
        try {
            boolean kodeRuangIsUsed = ruangService.cekIsEmptyKode(ruangForm.getKodeRuang());
            if (kodeRuangIsUsed) {
                actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.CODE_IS_USED));
                saveMessages(request, actionMessages);
                return mapping.findForward(SISScreenConstants.RUANG_EDIT);
            } else {
                Ruang ruang = ruangService.loadById(ruangForm.getId());
                ruang.setKodeRuang(ruangForm.getKodeRuang());
                ruang.setNamaRuang(ruangForm.getNamaRuang());
                ruang.setDeskripsi(ruangForm.getDeskripsi());
                ruangService.updateRuang(ruang);
                ruangForm.reset(mapping, request);
            }
        } catch (Exception ex) {
            Logger.getLogger(RuangAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.RUANG);
    }

    public ActionForward ruangDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        RuangService ruangService = (RuangService) CallCtx.getContext(request).getBean(SISBeanConstants.RUANG_SERVICE);
        RuangForm ruangForm = (RuangForm) form;
        try {
            Ruang ruang = ruangService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            ruangService.deleteRuang(ruang);
        } catch (Exception ex) {
            Logger.getLogger(RuangAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.RUANG);
    }
}